package com.hhsj.dynamicui.server.servlet.admin.consts;


import com.hhsj.androidserver.HttpServlet;
import com.hhsj.androidserver.HttpServletRequest;
import com.hhsj.androidserver.HttpServletResponse;
import com.hhsj.androidserver.WebServlet;
import com.hhsj.dynamicui.model.base.BaseModel;
import com.hhsj.dynamicui.server.DynamicUIService;

import java.net.URLDecoder;
import java.sql.PreparedStatement;

/**
 * 删除常量
 */
@WebServlet("/uhomecp-admin/const_delete")
public class DeleteConstsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        doPost(req, rep);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        //1.解析
        String const_code = URLDecoder.decode(req.getParameterValues("const_id")[0], "utf-8");

        String sql = String.format("DELETE * FROM const_type WHERE const_code=\'%s\'", const_code);

        try {
            PreparedStatement statement = DynamicUIService.getInstance().getConnection().prepareStatement(sql);
            boolean sqlReust = statement.execute();
            //2.删除数据
            if (sqlReust) {
                //3.返回响应结果
                rep.print(BaseModel.newEmpty("0", "成功").toJSONString());
            } else {
                rep.print(BaseModel.newEmpty("-1", "失败").toJSONString());
            }
        } catch (Exception e) {
            //3.返回响应结果
            rep.print(BaseModel.newEmpty("-1", "失败:" + e.getLocalizedMessage()).toJSONString());
        }


    }
}
